const app = getApp()
import { getProjectIslandMaterial, getIslandSpaceList, updateProjectIslandMaterial, getProjectMaterial, getProjectSpaceCategory, updateProjectMaterial } from '../../../../api/index'

Page({
  data: {
    projectId: '',
    type: '',
    id: '',
    businessType: '',
    businessTypeTxt: '',
    spaceId: '',
    detail: {},
    spaceList: []
  },
  async save () {
    try {
      let query = { ...this.data.detail }
      query.materialCode = query.code + query.materialCode
      if (this.data.type === 'islandProject') {
        await updateProjectIslandMaterial({ ...query })
      } else {
        await updateProjectMaterial({ ...query })
      }
      wx.showToast({ title: '信息更新成功！', icon: 'none' })
      wx.$nav.navigateBack()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeSpace ({ detail: { value } }) {
    let item = this.data.spaceList[value]
    this.setData({
      'detail.spaceId': item.spaceId,
      'detail.spaceName': item.spaceName
    })
  },
  changeInput ({ currentTarget: { dataset: { key } }, detail }) {
    this.setData({ ['detail.' + key]: detail.value })
  },
  async getIslandMaterial() {
    try {
      const { ResultSet } = await getProjectIslandMaterial({ projectId: this.data.projectId, businessType: this.data.businessType, selectId: this.data.id })
      this.setData({ detail: ResultSet.userData.data[0] })
      this.getIslandSpaceList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getIslandSpaceList () {
    try {
      const { ResultSet } = await getIslandSpaceList({ projectId: this.data.projectId, businessType: this.data.businessType, businessTypeText: this.data.businessTypeTxt, type: 1 })
      this.setData({ spaceList: ResultSet.userData.result })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getProjectMaterial () {
    try {
      let query = {
        projectId: this.data.projectId,
        projectMaterialId: this.data.id
      }
      if (this.data.spaceId) query.spaceId = this.data.spaceId
      const { ResultSet } = await getProjectMaterial({ ...query })
      this.setData({ detail: ResultSet.userData.data[0] })
      this.getProjectSpaceCategory()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getProjectSpaceCategory () {
    try {
      const { ResultSet } = await getProjectSpaceCategory({ projectId: this.data.projectId })
      this.setData({ spaceList: ResultSet.userData.projectSpace })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ projectId, type, id, businessType, businessTypeTxt, spaceId }) {
    if (type === 'islandProject') {
      this.setData({ projectId, type, id, businessType, businessTypeTxt })
      this.getIslandMaterial()
    } else {
      this.setData({ projectId, id, spaceId })
      this.getProjectMaterial()
    }
  }
})